package com.androdro.bluenote.coolstruttin1588;
/**
 * Cool Struttin'
 * CoolStruttin.java
 *
 * @author Hiroaki Sakuma <sakuma@androdro.com>
 * @version $Id$
 *
 * Copyright (C) androdro lab. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of the nor the names of its contributors may be used to
 *   endorse or promote products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.androdro.bluenote.coolstruttin1588.exception.*;

public class CoolStruttin
	extends
		Activity
	implements
		CoolStruttin1588,
		OnClickListener,
		OnTouchListener {


	private static Context c;
	private Display display;
	private DisplayMetrics metrics;
	private WindowManager wm;
	private ViewGroup vg;
	private Lover bg;
	private Thread t;

	private Map<View, Object> onclickitems;
	private SippinAtBells.PageManager pageManager;
	private FrameLayout f;
	private SippinAtBells.Infoticker infoticker;
//	private ArrayList<HashMap> actionstack;
//	private Integer currentaction;


	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		try {

			c = getApplicationContext();

			this.metrics = getResources().getDisplayMetrics();
			this.metrics.densityDpi = 320;
			this.metrics.widthPixels = 640;
			this.metrics.heightPixels = 960;
			this.metrics.xdpi = 320.0f;
			this.metrics.ydpi = 320.0f;
//			this.metrics.setTo(this.metrics);

			onclickitems = new HashMap<View, Object>();

			this.f = new FrameLayout(this);
			this.f.setBackgroundColor(Color.WHITE);

			setContentView(this.f);

			TextView v = new TextView(this);
			v.setBackgroundColor(Color.BLACK);
			v.setTextColor(0xffa4c639);
			v.setText("Loading JAM JAM.....");

			this.infoticker = SippinAtBells.getInstance().getFactory().getInfoticker(this);
			this.f.addView(v, 0, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
			this.f.addView(this.infoticker, 1, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

		} catch (Exception e) {
			Toast.makeText(this, e.toString() + "on " + getClass().toString() + " " + e.getStackTrace()[0].getMethodName(), Toast.LENGTH_LONG).show();
		}


	}


	@Override
	protected void onStart() {

		super.onStart();

		try {

			this.bg = Lover.getInstance(this);
			this.bg.start();

			this.pageManager = SippinAtBells.getInstance().getFactory().getPageManager();

			go(ACTION_INTRO);

		} catch (Exception e) {
			Toast.makeText(this, e.toString() + "on " + getClass().toString() + " " + e.getStackTrace()[0].getMethodName(), Toast.LENGTH_SHORT).show();
		}


	}


	@Override
	protected void onStop() {
		super.onStop();
		this.bg.stop();
		finish();
	}


	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		this.wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		this.display = wm.getDefaultDisplay();
	}


	public void go(int action, String param) {

		View v;

		switch(action & 0xfffff000) {
			case ACTION_INTRO:
				v = new DeepNight(this);
				break;
			case ACTION_MAP:
				v = BlueMinorFactory.getYou(this).getMap(param);
				break;
			case ACTION_ABOUT:
				v = RoyalFlashFactory.getYou(this).getInfo();
				break;
			case ACTION_INFO:
				v = RoyalFlashFactory.getYou(this).getInfo(param);
				break;
			default:
				return;
		}

		this.f.removeViewAt(0);
		this.f.addView(v, 0, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

		v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), 30);

		this.pageManager.newPage(action, v, param);

	}


	public void go(int action) {
		go(action, "");
	}


	public void go(HashMap<String, Object> actionmap) {

		View v = (View)actionmap.get("view");
		this.f.removeViewAt(0);
		this.f.addView(v, 0, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

		this.pageManager.newPage(actionmap);

	}


	public Display getDisplay() {
		return this.display;
	}


	public DisplayMetrics getDisplayMetrics() {
		return this.metrics;
	}


	public JSONObject getConfig() {
		return this.bg.getConfig();
	}


	public Lover getBackground() {
		return this.bg;
	}


	@Override
	public boolean dispatchKeyEvent(KeyEvent e) {

		if (e.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			switch (e.getAction()) {

				case KeyEvent.ACTION_DOWN:
					try {
						go(this.pageManager.getPrevPage());
					} catch(SippinAtBells.YouAreIntroException tiie) {
						onStop();
					} catch(SippinAtBells.ThisIsIntroException tiie) {
						onStop();
					}
					return true;

			}
		}

		return false;

	}


	public void putOnClickItem(View v, Object obj) {

		this.onclickitems.put(v, obj);

	}


	public void onClick(View v) {

	}


	public boolean onTouch(View v, MotionEvent event) {

		return false;

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		boolean r = super.onCreateOptionsMenu(menu);

		menu.add(OPTION_MENU_PREFERENCE, OPTION_MENU_PREFERENCE, 0, "Preference").setIcon(android.R.drawable.ic_menu_preferences);
		menu.add(OPTION_MENU_ABOUT, OPTION_MENU_ABOUT, 1, "About").setIcon(android.R.drawable.ic_menu_info_details);

		return r;

	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
			case OPTION_MENU_PREFERENCE:
				break;
			case OPTION_MENU_ABOUT:
				go(ACTION_ABOUT);
				return true;
		}

		return false;

	}


	public void showError(String error) {
		System.err.println(error);
		Toast.makeText(this, error, Toast.LENGTH_LONG).show();
	}


	public void showError(Exception error) {
		error.printStackTrace();
		Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show();
	}


}
